﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Data.SqlClient;

namespace FYPHP
{
    public partial class CategoryEdit : System.Web.UI.Page
    {
        SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["fyphp"].ConnectionString);

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                SqlCommand cmdSearch;
                SqlDataReader dtrSearch;
                conn.Open();

                cmdSearch = new SqlCommand("SELECT * FROM Category WHERE cat_id = @id", conn);
                cmdSearch.Parameters.AddWithValue("@id", Request.QueryString["id"]);
                dtrSearch = cmdSearch.ExecuteReader();

                dtrSearch.Read();
                txtCat.Text = dtrSearch["cat_name"].ToString();

                dtrSearch.Close();
                conn.Close();
            }
        }

        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            SqlCommand cmdIn;
            conn.Open();

            cmdIn = new SqlCommand("UPDATE Category SET cat_name=@name WHERE cat_id=@id", conn);
            cmdIn.Parameters.AddWithValue("@name", txtCat.Text);
            cmdIn.Parameters.AddWithValue("@id", Request.QueryString["id"]);
            cmdIn.ExecuteNonQuery();

            conn.Close();
            MessageBox("Update Successfully.");
        }

        private void MessageBox(string msg)
        {
            Label lbl = new Label();
            lbl.Text = "<script language='javascript'>" + Environment.NewLine + "window.alert('" + msg + "')</script>";
            Page.Controls.Add(lbl);
        }

        private void Redirect(string msg)
        {
            Label lbl = new Label();
            lbl.Text = "<script language=\"javascript\">window.location='" + msg + "';</script>";
            Page.Controls.Add(lbl);
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            Redirect("CategoryEdit.aspx?id=" + Request.QueryString["id"]);
        }
    }
}